package com.cdream.basic.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.simple.SimpleDataSource;

import javax.sql.DataSource;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.SQLException;
import java.util.List;

/**
 * 临时工具类
 */
public class TempUtil {
    private static DataSource ds = new SimpleDataSource("jdbc:mysql://localhost:3306/fsm_zhengtai", "root", "root");

    public static void main(String[] args) throws SQLException, IOException {
        List<Entity> images = Db.use(ds).findAll("fsmEquipmentImage");
        System.out.println(images);
        for (Entity image : images) {
            String imageUrl = image.getStr("imageUrl");
            String name = image.getStr("spec")+"_"+image.getStr("controlState")
                    +"_"+image.getStr("swtichState")+"_"+image.getStr("leakage");
            InputStream stream = URLUtil.getStream(new URL(imageUrl));
            String imagePath = "/Users/apple/Desktop/tempImage/" + name + "." + StrUtil.subAfter(imageUrl, ".", true);
            File file = new File(imagePath);

            FileOutputStream fileOutputStream = new FileOutputStream(file);
            IoUtil.copy(stream,fileOutputStream);
            stream.close();
            fileOutputStream.close();
        }

    }
}
